# -*- coding: utf-8 -*-
from xTkinter import *
import tkFileDialog
import tkMessageBox as msgBox
import sys
from Validation_Contraintes import *
Validateur = ValidationContrainte()
import xmlrpclib
class Vue_EditionRapport(Frame):
    def __init__(self,master,parent,**cnf):
        self.ListeChamps=[]
        self.ListeTable = []
        self.ListEntries=[]
        self.ListLabels=[]
        self.ListeContraintes = []
        self.ListeJointures = []
        self.parent = parent
        self.master=master
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=1600,height=1200)  
        self.Regle()
        self.ListeTables = []
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(25,25)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirChamps)
        self.CBTables.setText('<Choisir>')
        
        self.CBChamps=xCombobox(self)
        self.CBChamps.setLocation(140,25)
        self.CBChamps.setWidth(15)
        self.CBChamps.setListWidth(25)
        self.CBChamps.setListHeight(5)
        self.CBChamps.setValidator(None)
        self.CBChamps.setText('<Choisir>')
        
        self.CBJointureGauche=xCombobox(self)
        self.CBJointureGauche.setLocation(350,25)
        self.CBJointureGauche.setWidth(15)
        self.CBJointureGauche.setListWidth(25)
        self.CBJointureGauche.setListHeight(5)
        self.CBJointureGauche.setValidator(None)
        self.CBJointureGauche.setText('<Choisir>')
        
        self.CBJointureDroite=xCombobox(self)
        self.CBJointureDroite.setLocation(460,25)
        self.CBJointureDroite.setWidth(15)
        self.CBJointureDroite.setListWidth(25)
        self.CBJointureDroite.setListHeight(5)
        self.CBJointureDroite.setValidator(None)
        self.CBJointureDroite.setText('<Choisir>')
        
        
        self.Bouton=xButton(self,text="Ajouter Champ",width=12, command=self.AjouterChamp)
        self.Bouton.setLocation(260,25)
        
        self.Bouton=xButton(self,text="Ajouter Jointure",width=14, command=self.AjouterJointure)
        self.Bouton.setLocation(575,25)
        
        self.Bouton=xButton(self,text="Ajouter Rapport",width=14, command=self.AjouterRapport)
        self.Bouton.setLocation(575,25)
        
        self.TamponTable =  {}
        self.ListLabels = []
        self.ListEntries = []
        self.Champs = []
        self.index = 0
        
    def AjouterChamp(self):
        self.Champs.append(self.CBTables.getText() + "." + self.CBChamps.getText())
        self.TamponTable[self.CBTables.getText()].remove(self.CBChamps.getText())
        text = self.CBTables.getText() + "." + self.CBChamps.getText()
        if self.CBTables.getText() not in self.ListeTable:
            self.ListeTable.append(self.CBTables.getText())
        self.CBJointureDroite.addItem(text)
        self.CBJointureGauche.addItem(text)
        self.ListLabels.append(xLabel(self,text=text, width=len(text)+2, anchor=SW ))
        self.ListLabels[len(self.ListLabels)-1].setLocation(25,(self.index+1)*25+25)
        self.ListEntries.append(xEntry(self,width=24))
        self.ListEntries[len(self.ListEntries)-1].setLocation(175,(self.index+1)*25+25)
        self.ListEntries[len(self.ListEntries)-1].setText(self.CBChamps.getText())
        self.index = self.index + 1
        self.obtenirChamps()
        pass
    
    def AjouterJointure(self):
        pass
    
    def AjouterRapport(self):
        Requete = "select "
        for champ in self.Champs:
            Requete = Requete + champ + ", "
        Requete = Requete[:-2] + " from "
        for table in self.ListeTable:
            Requete = Requete + table + ", "
        Requete = Requete[:-2]
        print Requete
        RequeteInsert = "insert into PERP_RAPPORT " + "'" + Requete + "'"
        print RequeteInsert

    
    def obtenirTables(self):
        column=0 
        Reponse = self.parent.serveur.ObtenirListeTables()
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i])
        
    def obtenirChamps(self):
        self.CBChamps.clear()
        self.CBChamps.setListHeight(5)
        table=self.CBTables.getText()
        if self.TamponTable.has_key(self.CBTables.getText()):
            ListeChamp = self.TamponTable[table]
        else:
            Reponse = self.parent.serveur.ObtenirListeChamps(table)
            if(Reponse[0]==1):
                ListeChamp = []
                for Champs in Reponse[1]:
                    ListeChamp.append(Champs[0])
                self.TamponTable[table] = ListeChamp
                print self.TamponTable[table]
            else:
                msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
                return
        if ListeChamp == []:
             self.CBChamps.setText("vide")
        for Champs in ListeChamp:
            self.CBChamps.addItem(Champs)
        
        
    def close(self):
        self.destroy()
        pass

    def Regle(self):
        for x in range (1600/25):
            X = x*25
            xLabel(self, text=str(X)).setLocation(X,0)
        for y in range (1200/25):
            Y = y*25
            xLabel(self, text=str(Y)).setLocation(0,Y)
    
    
            
class Controleur:
    def __init__(self):
        Serveur='http://localhost:8000'
        self.serveur=xmlrpclib.ServerProxy(Serveur)
        root=Tk()
        root.geometry('800x400')
        vi=Vue_EditionRapport(root,self)
        root.mainloop()       

Controleur = Controleur()

